﻿/*
 * Creato da SharpDevelop.
 * Utente: Ciccio
 * Data: 01/05/2008
 * Ora: 20.19
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Controls;
using ACM.cmsarea08.Administration;
using ACM.cmsarea08.Administration.Controls;


namespace ACM.cmsarea08.Administration.Core
{
	/// <summary>
	/// Description of Class1.
	/// </summary>
	
	public class DataGridManager
	{
		private string[] DgridValue = new string[2];
		private System.Web.UI.Page Page;
		
		public string SelectedColumnName {
			get {
				if(DgridValue.Length>0 && (!(string.IsNullOrEmpty(DgridValue[0])) ) ) 
					return DgridValue[0];
				else
					return string.Empty;
			}
		}
		public string SelectedValue {
			get {
				if(DgridValue.Length>1 && (!(string.IsNullOrEmpty(DgridValue[1])) ) ) 
					return DgridValue[1];
				else
					return string.Empty;
			}
		}
		
		private ACM.cmsarea08.Controls.Itan_DataGrid CurrentDataGrid;
		
		public DataGridManager(System.Web.UI.Page page)
		{
			DgridValue = new string[2];
			CurrentDataGrid = null;
			this.Page = page;
		}
		
		public bool GetDataGrid() {
			if(CurrentDataGrid!=null) {
				DgridValue[1] = CurrentDataGrid.SelectedRow;
				DgridValue[0] = CurrentDataGrid.SelectColumnField;				
				return true;
			}
			for(int i =0; i<this.Page.Controls.Count;i++) {
					CercaControllo(this.Page.Controls[i]);	
					if(CurrentDataGrid!=null) {
						break;
					}
			}
			if(CurrentDataGrid!=null) {
				DgridValue[1] = CurrentDataGrid.SelectedRow;
				DgridValue[0] = CurrentDataGrid.SelectColumnField;				
				return true;
			} else
				return false;			
		}
		
//		public object getSelectedDataRowValue(string columnname) {
//			bool isDataGrid = false;
//			if(CurrentDataGrid==null)
//				isDataGrid = GetDataGrid();
//			else
//				isDataGrid=true;
//			
//			if(isDataGrid==false ) { 
//				HttpContext.Current.Response.Write( "NO DGRID<br/>");
//				return null;
//			}
//			if(CurrentDataGrid.DataSource == null) { 
//				HttpContext.Current.Response.Write( "NO DSOURCE<br/>");
//				return null;
//			}
//			HttpContext.Current.Response.Write( CurrentDataGrid.DataSource.GetType() + "<br/>");
//			
//			if(CurrentDataGrid.DataSource is DataView) {
//				if( ((DataView)CurrentDataGrid.DataSource).Table==null) return null;
//				for(int i=0; i < ((DataView)CurrentDataGrid.DataSource).Table.Rows.Count; i++) {
//					if(((DataView)CurrentDataGrid.DataSource).Table.Rows[i][this.SelectedColumnName].ToString() == this.SelectedValue) {
//						//cerca valore:
//						HttpContext.Current.Response.Write("TROVATA RIGA: ");
//						if ( ((DataView)CurrentDataGrid.DataSource).Table.Columns.Contains(columnname)) 
//							return ((DataView)CurrentDataGrid.DataSource).Table.Rows[i][columnname];
//					}
//				}
//				
//				
//			}
//			return null;
//		}
		
		private void CercaControllo(Control ctrl) {
			if(ctrl is ACM.cmsarea08.Controls.Itan_DataGrid) {
				CurrentDataGrid = (ACM.cmsarea08.Controls.Itan_DataGrid)ctrl;
					return; // valori;
			} 
			if(ctrl.Controls.Count>0) {
				for(int i = 0; i<ctrl.Controls.Count;i++)
					CercaControllo(ctrl.Controls[i]);
			}
			return;
		}
			
			
		
		
		
	}
}
